﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using MODEL;
using Service;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using System.Windows.Input;
using GalaSoft.MvvmLight.CommandWpf;
using Common;

namespace ViewModel.Agriculture
{
    public class AgricultureViewModel : ViewModelBase
    {
        private IAgricultureService m_AgricultureService;

        private AgricultureQueryCriteria m_QueryCriteria;
        public AgricultureQueryCriteria QueryCriteria
        {
            get { return this.m_QueryCriteria; }
            set { this.m_QueryCriteria = value; base.RaisePropertyChanged(() => QueryCriteria); }
        }

        public ICommand AgricultureInfosSearchFunc { get; set; }
        public ICommand AgricultureInfoCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand AgriInfoSaveCommand { get; set; }

        private ObservableCollection<AgricultureInfo> m_AgricultureInfos;
        public ObservableCollection<AgricultureInfo> AgricultureInfos
        {
            get { return this.m_AgricultureInfos; }
            set { this.m_AgricultureInfos = value; base.RaisePropertyChanged(() => AgricultureInfos); }
        }

        public AgricultureViewModel(IAgricultureService agricultureService)
        {
            this.m_AgricultureService = agricultureService;
            this.QueryCriteria = new AgricultureQueryCriteria();
            AgricultureInfosSearchFunc = new RelayCommand(SearchAgricultureInfos);
            AgricultureInfoCommand = new RelayCommand<AgricultureInfo>(ShowAgricultureInfo);
            EditCommand = new RelayCommand<AgricultureInfo>(EditInfo);
            DeleteCommand = new RelayCommand<AgricultureInfo>(DeleteInfo);
            AgriInfoSaveCommand = new RelayCommand(SaveInfo);
            RefreshData();
        }

        private void SaveInfo()
        {
            int a;
            a = 0;
        }

        private void EditInfo(AgricultureInfo agricultureinfo)
        {
            AgricultureDetailViewModel vm = new AgricultureDetailViewModel { AgricultureInfo = agricultureinfo };
            MessengerInstance.Send(vm, "Agriculture_EditInfoForm");
        }

        private void DeleteInfo(AgricultureInfo agricultureinfo)
        {
            
        }

        private void ShowAgricultureInfo(AgricultureInfo agricultureinfo)
        {

            AgricultureDetailViewModel vm = new AgricultureDetailViewModel { AgricultureInfo = agricultureinfo };
            MessengerInstance.Send(vm, "Agriculture_ShowInfoForm");
        }

        public async void SearchAgricultureInfos()
        {
            try
            {
                IEnumerable<AgricultureInfo> agricultureInfos = await this.m_AgricultureService.QueryAgricultureInfosAsync(this.QueryCriteria);
                this.AgricultureInfos = new ObservableCollection<AgricultureInfo>(agricultureInfos);
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("加载数据失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }

        private void RefreshData()
        {
            SearchAgricultureInfos();
        }
    }
}
